package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * @author xujianhong
 * @since 2021-08-06
 */
public class ZoomOutSlideTransformer implements BannerViewPager.PageTransformer {
    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float height = (float)page.getHeight();
        float width = (float)page.getWidth();
        float scaleFactor = 0F;
        float vertMargin = 0F;
        float horzMargin = 0F;

        switch (pageType) {
            case PREVIOUS:
                offset = 1 - offset;
                if(offsetPixels < 0){
                    scaleFactor = Math.max(0.85F, (float)1 - Math.abs(offset));
                    vertMargin = height * ((float)1 - scaleFactor) / (float)2;
                    horzMargin = width * ((float)1 - scaleFactor) / (float)2;
                    page.setPivotY(0.5F * height);
                    page.setPivotX(0.5F * width);
                    page.setTranslationX(horzMargin - vertMargin / (float)2);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setAlpha(0.5F + (scaleFactor - 0.85F) / 0.14999998F * 0.5F);
                }else{
                    page.setAlpha(1F);
                    page.setTranslationX(1F);
                }
                break;
            case CURRENT:
                scaleFactor = Math.max(0.85F, (float)1 - Math.abs(offset));
                vertMargin = height * ((float)1 - scaleFactor) / (float)2;
                horzMargin = width * ((float)1 - scaleFactor) / (float)2;
                page.setPivotY(0.5F * height);
                page.setPivotX(0.5F * width);
                if(offsetPixels>0){
                    page.setTranslationX(horzMargin - vertMargin / (float)2);
                }else{
                    page.setTranslationX(-horzMargin + vertMargin / (float)2);
                }
                page.setScaleX(scaleFactor);
                page.setScaleY(scaleFactor);
                page.setAlpha(0.5F + (scaleFactor - 0.85F) / 0.14999998F * 0.5F);
                break;
            case NEXT:
                offset = offset - 1 ;
                if(offsetPixels >= 0){
                    scaleFactor = Math.max(0.85F, (float)1 - Math.abs(offset));
                    vertMargin = height * ((float)1 - scaleFactor) / (float)2;
                    horzMargin = width * ((float)1 - scaleFactor) / (float)2;
                    page.setPivotY(0.5F * height);
                    page.setPivotX(0.5F * width);
                    page.setTranslationX(-horzMargin + vertMargin / (float)2);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setAlpha(0.5F + (scaleFactor - 0.85F) / 0.14999998F * 0.5F);
                }else{
                    page.setAlpha(1F);
                    page.setTranslationX(1F);
                }
                break;
            case OTHER:
                page.setAlpha(1F);
                page.setTranslationX(1F);
                break;
        }
    }
}